﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScriptIT
{
    public partial class frmDatabases : Form
    {
        Databases dataBases;
        bool bAddNew;

        internal Databases DataBases
        {
            get { return dataBases; }
            set { dataBases = value; }
        }
        public frmDatabases()
        {
            InitializeComponent();
        }

        private void frmDatabases_Load(object sender, EventArgs e)
        {
            if (dataBases == null)
            {
                dataBases = new Databases();
            }
            dgDatabases.DataSource = dataBases.AsList;
            dgDatabases.Refresh();
            SetButtonState(false);
        }

        private void dgDatabases_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            Database selectedRow = (Database)dgDatabases.Rows[e.RowIndex].DataBoundItem;
            txtDBName.Text = selectedRow.Name;
            txtDBDescription.Text = selectedRow.Description;
            txtConnectionString.Text = selectedRow.ConnectionString;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Database selectedDB;
            if (bAddNew)
            {
                selectedDB = new Database();
                selectedDB.Name = txtDBName.Text;
                selectedDB.Description = txtDBDescription.Text;
                selectedDB.ConnectionString = txtConnectionString.Text;
                dataBases.Add(selectedDB);
            }
            else
            {
                selectedDB = (Database)dgDatabases.SelectedRows[0].DataBoundItem;
                selectedDB.Name = txtDBName.Text;
                selectedDB.Description = txtDBDescription.Text;
                selectedDB.ConnectionString = txtConnectionString.Text;
                dataBases.Edit(selectedDB);
            }
            dgDatabases.DataSource = null;
            dgDatabases.DataSource = dataBases.AsList;
            dgDatabases.Refresh();
            //SelectDBInGrid(selectedDB);
            SetButtonState(false);
            bAddNew = false;
        }

        private void SelectDBInGrid(Database selectedDB)
        {
            DataGridViewRow selecteddgr = null;
            foreach (DataGridViewRow dgr in dgDatabases.Rows)
            {
                if (((Database)dgr.DataBoundItem).Key == selectedDB.Key)
                {
                    selecteddgr = dgr;
                    break;
                }
            }
        }

        private void tsAdd_Click(object sender, EventArgs e)
        {
            bAddNew = true;
            txtDBName.Text = string.Empty;
            txtDBDescription.Text = string.Empty;
            txtConnectionString.Text = string.Empty;
            SetButtonState(true);
        }
        private void SetButtonState(bool isEdit)
        {
            if (isEdit)
            {
                btnOK.Visible = true;
                btnConnectionString.Enabled = true;
                btnCancel.Text = "Cance&l";
                dgDatabases.Enabled = false;
                txtDBName.ReadOnly = false;
                txtDBDescription.ReadOnly = false;
                txtConnectionString.ReadOnly = false;
            }
            else
            {
                btnOK.Visible = false;
                btnConnectionString.Enabled = false;
                btnCancel.Text = "C&lose";
                dgDatabases.Enabled = true;
                txtDBName.ReadOnly = true;
                txtDBDescription.ReadOnly = true;
                txtConnectionString.ReadOnly = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (btnCancel.Text == "Cance&l")
            {
                if (dgDatabases.SelectedRows.Count > 0)
                {
                    Database selectedRow = (Database)dgDatabases.SelectedRows[0].DataBoundItem;
                    txtDBName.Text = selectedRow.Name;
                    txtDBDescription.Text = selectedRow.Description;
                    txtConnectionString.Text = selectedRow.ConnectionString;
                }
                SetButtonState(false);
                bAddNew = false;
            }
            else
            {
                this.Close();
            }
        }

        private void tsDelete_Click(object sender, EventArgs e)
        {
            Database selectedRow = (Database)dgDatabases.SelectedRows[0].DataBoundItem;
            dataBases.Remove(selectedRow);
        }

        private void btnConnectionString_Click(object sender, EventArgs e)
        {
            Microsoft.Data.ConnectionUI.DataConnectionDialog _dialog = new Microsoft.Data.ConnectionUI.DataConnectionDialog();

            //Microsoft.Data.ConnectionUI.DataSource.AddStandardDataSources(_dialog);
            _dialog.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.SqlDataSource);
            _dialog.SetSelectedDataProvider(Microsoft.Data.ConnectionUI.DataSource.SqlDataSource, Microsoft.Data.ConnectionUI.DataProvider.SqlDataProvider);
            Microsoft.Data.ConnectionUI.DataConnectionDialog.Show(_dialog);
            if (_dialog.DialogResult == DialogResult.OK)
            {
                txtConnectionString.Text = _dialog.ConnectionString;
                if (txtDBName.Text == string.Empty)
                    txtDBName.Text = GetDBName(txtConnectionString.Text);
            }
        }
        private string GetDBName(string connectionString)
        {
            string sDBName = string.Empty;
            string sSrvName = string.Empty;
            string[] sStr = connectionString.Split(Convert.ToChar(";"));
            foreach (string sSubStr in sStr)
            {
                if (sSubStr.Contains("Data Source"))
                {
                    sSrvName = sSubStr.Substring(sSubStr.IndexOf(Convert.ToChar("=")) + 1);
                }
                if (sSubStr.Contains("Initial Catalog"))
                {
                    sDBName = sSubStr.Substring(sSubStr.IndexOf(Convert.ToChar("=")) + 1);
                }
            }
            return sDBName + "@" + sSrvName;
        }

        private void tsEdit_Click(object sender, EventArgs e)
        {

        }
    }
}
